package com.jeesite.modules.utils.wx.vo;

import com.jeesite.common.io.IOUtils;
import com.jeesite.common.lang.StringUtils;
import org.dom4j.Attribute;

import lombok.Data;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;


@Data
public class WxTuisong {
    private String toUserName;
    private String fromUserName;
    private String createTime;
    private String msgType;
    private String event;


    /**
     * xml解析器
     * @param xml xml字符串
     * @param filterRootEleName 匹配的根节点名
     * @return
     * @返回map，格式：键：值
     * @返回属性值：->节点名=>属性名：属性值
     * @返回节点值：->节点名：值
     * @返回子节点属性值：->节点名->子节点名=>属性名：属性值
     * @返回子节点值：->节点名->子节点名：属性值
     */
    public static Map<String,Object> xmlParser(String xml, String filterRootEleName){
        Map<String,Object> retMap = new HashMap<>();
        //1.创建Reader对象
        try {
            SAXReader reader = new SAXReader();
            InputStream targetStream = IOUtils.toInputStream(xml, StandardCharsets.UTF_8.name());
            Document document = reader.read(targetStream);
            //3.获取根节点
            Element rootElement = document.getRootElement();
            StringBuilder builder = new StringBuilder();
            parser(rootElement,builder,filterRootEleName,retMap);
        }catch (Exception ex){
            ex.printStackTrace();
        }
        return retMap;
    }

    /**
     * xml递归解析器
     * @param ele 解析节点
     * @param eleKey 上级节点key
     * @param retMap 返回map
     */
    private static void parser(Element ele, StringBuilder eleKey, String firstEleName, Map<String,Object> retMap){
        StringBuilder builder = new StringBuilder(eleKey.toString());
        if(StringUtils.isEmpty(firstEleName)
                || firstEleName.equals(ele.getName())){
            firstEleName = null;
            builder.append("->"+ele.getName());
            if(org.apache.commons.lang3.StringUtils.isNotEmpty(org.apache.commons.lang3.StringUtils.stripToEmpty(ele.getData()+""))){
                retMap.put(builder.toString(), org.apache.commons.lang3.StringUtils.stripToEmpty(ele.getData()+""));
            }
            List<Attribute> attributes = ele.attributes();
            for (Attribute attribute : attributes) {
                StringBuilder builder1 = new StringBuilder(builder.toString());
                builder1.append("=>"+attribute.getName());
                if(StringUtils.isNotEmpty(attribute.getValue())){
                    retMap.put(builder1.toString(),attribute.getValue());
                }
            }
        }
        Iterator iterator1 = ele.elementIterator();
        while (iterator1.hasNext()){
            Element eleChild = (Element) iterator1.next();
            parser(eleChild,builder,firstEleName,retMap);
        }
    }


    public static void main(String[] args) {
        String xml = "<xml><ToUserName><![CDATA[gh_e31226b032ee]]></ToUserName><FromUserName><![CDATA[oHAt-v-VKTNH1f9duy5Vb6hiEkB8]]></FromUserName><CreateTime>1722948848</CreateTime><MsgType><![CDATA[event]]></MsgType><Event><![CDATA[subscribe]]></Event><EventKey><![CDATA[]]></EventKey></xml>";
        try {
            Map<String,Object> map = xmlParser(xml,"");
            System.out.println(map.get("->xml->FromUserName"));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
